package com.chf.springboot_test_thymeleaf.controller;

import com.chf.springboot_test_thymeleaf.beans.Student;
import com.chf.springboot_test_thymeleaf.service.Impl.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class UserController {

    @Autowired
    UserService userService;
    //登录
    @RequestMapping("/login")
    public String Login(Student student){
        boolean bool = userService.Login(student);
        System.out.println(student);
        if (bool){
            return "redirect:/query";
        }
        return "index";
    }
    //查询所有信息
    @RequestMapping("/query")
    public String Query(Model model){
        List<Student> lists = userService.QueryAllUserInfo();
        model.addAttribute("students",lists);
        return "User/userInfo";
    }
    //ID查询信息
    @RequestMapping("/queryById/{id}")
    public String QueryById(@PathVariable("id") Integer id, Model model){
        Student student = userService.QueryUserInfo(id);
        model.addAttribute("student",student);
        return "User/info";
    }

    //添加信息的跳转
    @RequestMapping("/goForm")
    public String GoForm(Model model){
        model.addAttribute("student",new Student());
        return "User/form";
    }
    //修改信息的跳转
    @RequestMapping("/goForm/{id}")
    public String GoFormID(@PathVariable("id") Integer id,Model model){
        Student student = userService.QueryUserInfo(id);
        model.addAttribute("student",student);
        return "User/form";
    }

    //更新信息  添加与删除
    @RequestMapping("/update")
    public String UpdateUserInfo(Student student){
        if(student.getId()!=null){
            userService.UpdateUserInfo(student);
        }else {
            userService.InsertUserInfo(student);
        }
        return "redirect:/query";
    }

    //删除信息
    @RequestMapping("/deleteUserById/{id}")
    public String DelectUserInfo(@PathVariable("id") Integer id, Model model){
        userService.DeleteUserInfo(id);
        return "redirect:/query";
    }
}
